//
//  EXT+UIImage.swift
//  WB123
//
//  Created by xuyunde mac pro on 2017/6/29.
//  Copyright © 2017年 NSObjiect. All rights reserved.
//

import UIKit
/*
 - 通过代码方式来解决图片拉伸问题
 */
extension UIImage {
    // 通过class 来修饰类方法
    class func resizableImage(named:String) -> UIImage{
        // 图片
        let image = UIImage(named: named)!
        // 设置端盖的值
        let top = image.size.height * 0.5
        let left = image.size.width * 0.5
        let bottom = image.size.height * 0.5
        let right = image.size.width * 0.5
        let edgeInsets =  UIEdgeInsetsMake(top, left, bottom, right)
        // 拉伸图片
        let newImage = image.resizableImage(withCapInsets: edgeInsets)
        return newImage
    }
}

extension UIImage{
    // MARK: - 截屏方法
    class func getScreenSnap() -> UIImage?{
        
        //截取主屏幕的内容
        let window = UIApplication.shared.keyWindow!
        //开启上下文
        UIGraphicsBeginImageContext(window.bounds.size)
        // 将内容渲染到上下文
        window.drawHierarchy(in: window.bounds, afterScreenUpdates: false)
        // 从上下文获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        // 关闭上下文
        UIGraphicsEndImageContext()
        return image
    }
}
